#ifndef IHPLAPP_H
#define IHPLAPP_H
//---------------------------------------------------------------------------------------
#include <stdint.h>
#include <QWidget>
#include "BaseApp.h"
#include "Comm/TaskMan.h"
//---------------------------------------------------------------------------------------
namespace Ui {
class IHPLWidget;
}
class GlobalSetting;

#pragma pack(1)
struct LibHead
{
    uint8_t seq[2];
    uint8_t ack;
    uint8_t data[0];
};
#define LIBHEAD_SIZE sizeof(struct LibHead)
#define IRLIB_SEND_BLOCKSIZE (0x7f-LIBHEAD_SIZE)
struct LibHeader
{
    uint8_t len[4];
    uint8_t crc[2];
    uint8_t blocksize;
    uint8_t libno[2];
};
#pragma pack()
//---------------------------------------------------------------------------------------
class IHPLApp :  public BaseApp, public TaskInf
{
    Q_OBJECT

public:
    explicit IHPLApp(TaskMan *task_man, QWidget *parent = 0);
    ~IHPLApp();

private slots:
    void on_pushButton_upload_clicked();
    void on_pushButton_import_clicked();
    void on_pushButtonSelAll_clicked();
    void on_pushButtonDeSel_clicked();
    void on_pushButton_start_clicked();

private:
    Ui::IHPLWidget *ui;
    TaskMan *task_man;
    GlobalSetting *setting;

    class PrivateData;
    PrivateData *d_data;

    Task *task;
    virtual void OnTaskDone(Task *task);

    void update_ui(void);

    void get_lib_header(LibHead *head, int len);
    void get_lib_data(LibHead *head, int len);
    void get_lib_tail(void);

    void OnUploadTaskDone(PrivateData *arg, Task *task);
    void OnDownloadTaskDone(PrivateData *arg, Task *task);

    void ClearTableWidget(void);

    int GetNextRow(int from);
    int code_trans_packet(bool upload,
                           uint8_t *data, int len, uint8_t *out, int maxlen);

    int get_download_header(uint8_t *out, int maxlen);
    int get_download_data(int seq, uint8_t *out, size_t maxlen);
    int get_download_packet(int seq, uint8_t *out, size_t maxlen);

    int AddDownLoadTask(PrivateData *arg, int seq, bool nextRow);
    int ParseLibFile(const QString &path);
};

extern "C"
{
    BaseApp *CreateIHPLApp(QWidget *parent, TaskMan *task_man);
}
//---------------------------------------------------------------------------------------
#endif // IHPLWIDGET_H
